#include <base\plane.h>

plane::plane(vec3f &v1, vec3f &v2, vec3f &v3) {
	set3Points(v1,v2,v3);
}


plane::plane() {
}

plane::~plane() {
}

void 
plane::set3Points(vec3f &v1, vec3f &v2, vec3f &v3) {

	vec3f aux1, aux2;

	aux1 = v1 - v2;
	aux2 = v3 - v2;

	normal = aux2.cross(aux1);

	normal.normalize();
	point = v2;
	d = -(normal.dot(point));
}

void 
plane::setNormalAndPoint(vec3f &normal_, vec3f &point_) {

	normal = normal_;
	normal.normalize();
	d = -(normal.dot(point_));
}

void 
plane::setCoefficients(float a_, float b_, float c_, float d_) {

	// set the normal vector
	normal = vec3f(a_, b_, c_);
	//compute the lenght of the vector
	float l = normal.length();
	// normalize the vector
	normal = vec3f(a_ / l, b_ / l, c_ / l);
	// and divide d by th length as well
	d = d_ / l;
}

float 
plane::distance(vec3f &p) {
	return (d + normal.dot(p));
}
